//
//  UserAccount.m
//  微博
//
//  Created by 沈艳男 on 15/11/20.
//  Copyright © 2015年 沈艳男. All rights reserved.
//

#import "UserAccount.h"

// 设置保存归档信息的plist路径
#define AccounFilePath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingString:@"account.plist"]

@interface UserAccount()<NSCoding>
@property (nonatomic, strong) NSString *accoutnToken;

@end

@implementation UserAccount

+ (instancetype)sharedUserAccount
{
    static UserAccount *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    return instance;
}

+ (instancetype)usetAccountWithDict:(NSDictionary *)dict
{
    UserAccount *account = [[UserAccount alloc] init];
    [account setValuesForKeysWithDictionary:dict];
    return account;
}
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{}

- (instancetype)init
{
    self = [super init];
    if (self) {
        UserAccount *userAccount = [UserAccount readUserAccount];
        self.userAccount = userAccount;
        NSTimeInterval time = userAccount.expires_in.doubleValue;
        NSDate *newDate = [NSDate dateWithTimeIntervalSinceNow:time];
        self.expires_date = newDate;
    }
    return self;
}

#pragma mark - 用户登录标记
- (BOOL)userLogon
{
    return self.access_token != nil;
}

- (NSString *)access_token
{
    if (!_access_token) {
        _access_token = self.userAccount.access_token;
    }
    return _access_token;
}

- (NSString *)description
{
    return [NSString stringWithFormat:@"_access_token:%@\n _expires_in:%@\n _uid:%@\n _expires_date:%@\n _name:%@\n _avatar_large:%@\n", _access_token, _expires_in, _uid, _expires_date, _name, _avatar_large];
}

// 把对象归档到文件中，会调用一次
- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:self.access_token forKey:@"access_token"];
    [aCoder encodeObject:self.expires_in forKey:@"expires_in"];
    [aCoder encodeObject:self.expires_date forKey:@"expires_date"];
    [aCoder encodeObject:self.uid forKey:@"uid"];
    [aCoder encodeObject:self.avatar_large forKey:@"avatar_large"];
    [aCoder encodeObject:self.name forKey:@"name"];
}

- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super init]) {
        self.access_token = [aDecoder decodeObjectForKey:@"access_token"];
        self.expires_in = [aDecoder decodeObjectForKey:@"expires_in"];
        self.expires_date = [aDecoder decodeObjectForKey:@"expires_date"];
        self.uid = [aDecoder decodeObjectForKey:@"uid"];
        self.avatar_large = [aDecoder decodeObjectForKey:@"avatar_large"];
        self.name = [aDecoder decodeObjectForKey:@"name"];
    }
    return self;
}

// 保存数据
+ (void)saveUserAccount:(UserAccount *)account
{
    [NSKeyedArchiver archiveRootObject:account toFile:AccounFilePath];
    NSLog(@"%@", AccounFilePath);
}

// 读取数据
+ (UserAccount *)readUserAccount
{
    UserAccount *account = [NSKeyedUnarchiver unarchiveObjectWithFile:AccounFilePath];
    NSDate *date = account.expires_date;
    // 判断当前的accountToken的日期，如果大于当前日期就证明没有过期
    if ([date compare:[NSDate date]] ==  NSOrderedDescending) {
        return account;
    }
    NSLog(@"accountToken已过期");
    return nil;
}


@end
